/**
 * Created by xw2sy on 2017-05-03.
 */
var util={
    el:{
        success:function (msg) {
            util.el.alert(msg||"<i class='glyphicon glyphicon-ok text-success'></i>操作成功！");
        },
        error:function (msg) {
        	util.el.alert(msg||"<i class='glyphicon glyphicon-ok text-success'></i>操作失败！");
        },
        data:function (exp) {
           return $("input,:checkbox:checked,:radio:checked",$(exp)).serialize();
        },
        fillData:function (exp,data) {
            $("input",$(exp)).each(function (i,n) {
                var name=$(n).attr("name");
                if(name && data[name]!=undefined){
                    $(n).val(data[name]);
                }
            });
        },
        alert:function (msg) {
            $("#jsSmModalDialog .modal-body").html(msg);
            $("#jsSmModalDialog").modal('show');
        },
        confirm:function (el,onOk) {
        	var body=$("#jsConfirmDialog .modal-body");
        	var oldElId=body.data("contentEl");
        	if(oldElId){
        		$(oldElId).addClass("hidden");
        		$(oldElId).appendTo(document.body);
        	}
        	body.data("contentEl",el);
        	$(el).removeClass("hidden").appendTo(body);
            $("#jsConfirmDialog").modal('show');
            $("#jsConfirmDialogBtnOk").unbind("click").click(function () {
                $("#jsConfirmDialog").modal('hide');
                if(onOk){
                    onOk.call(this);
                }
            });
        }
    },
    request:{
        get:function (url) {
            url=(appRoot+url).replace(/\/+/g,"/");
            $.get(url,function (r) {
                alert("成功！");
            });
        },
        post:function (url,data,onSuccess,onFail) {
            var onSuccess=onSuccess||util.el.success;
            var onFail=onFail||util.el.error;
            url=(appRoot+url).replace(/\/+/g,"/");
            $.post(url,data,function (r) {
                if(r && r.result){
                    if(r.result==false) {
                        onFail.call(r,r.data);
                    } else {   onSuccess.call(r,r.data);}

                }else{
                    onFail.call(r,r.data.message || r.data);
                }
            });
        }
    }
}

//适应窗体高度
function fixWindowHeight(){
    if(window.top==window){
        return;
    }
    var _tb_tc=$(".table_content");
    var initHeight=_tb_tc.attr("initHeight");
    //还原成原始高度
    if(initHeight){
        _tb_tc.height(initHeight);
    }
    var tc_height=_tb_tc.height();
    var wd_height=$(window).height()-($(".panel-heading").height()+47);
    //记录原始高度
    _tb_tc.attr("initHeight",tc_height);
    if(tc_height<wd_height){
        //设置成合适高度
        _tb_tc.height(wd_height-7);
    }else{
        _tb_tc.height(tc_height>wd_height?wd_height:tc_height-60);
    }
}

//读卡
var AppCardRead={
    onCardRead:function(cardId) {

    }
};

$(function () {

    //设置下拉列表选中项
    $("select[value]").each(function(){
        $("option[value='"+$(this).attr("value")+"']",this).prop("selected",true);
    });

    if(!window["activeMenu"]){
        //中心管理打开的，要设置内容页高度
        fixWindowHeight();
        $(window).resize(fixWindowHeight);
        return;
    }
    $("#navbar li").removeClass("active");
    $("#navbar li."+activeMenu).addClass("active");

    //读卡
    if(!window["getCardIdInOneFrame"]){
        return;
    }
    //点击读卡
    $("#jsReadCard,#jsReadCard2").click(function () {
        var cardId=getCardIdInOneFrame()
        $(this).parent().prev().val(cardId);
        //非授权卡
        if($(this).is("#jsReadCard")){
            AppCardRead.onCardRead(cardId);
        }
    });
    
    
    //alert的标题那行点击，就关当前有没有遮罩，暂解 打印成功提示后，遮罩过多后续操作不了 20171026
     $("#jsSmModalDialog .modal-header .modal-title").click(function () {
     	  if($("#jsConfirmDialog:visible").length != 0){
           $("#jsConfirmDialog").modal('hide');
        }
    });
    
});


/**
 * 加载下拉列表插件
 *
 */
(function($){
    $.fn.ajaxSelect=function(ops){
        //默认参数
        var defaultOps={
            url:"",
            key:"",
            pre:"",
            all:false,
            option:null,
            end:function(){}
        };
        var sOps =$.extend(defaultOps,ops);
        return this.each(function(){
            var that=$(this);
            $.get(sOps.url,function(r){
                if(r.result){
                    r= r.data;
                    //展示全部
                    if(sOps.all){
                        r.splice(0, 0, {hall_id:"-1",hall_name:"全部"});
                    }
                    var _ops=sOps.pre;
                    for(var i=0; i<r.length; i++){
                        _ops+=sOps.option(r[i]);
                    }
                    that.html(_ops);
                    sOps.end();
                }
            });
        });
    };
})(jQuery)